package com.example.myclient;
import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class MainActivity extends Activity {
    // 定义s界面上的两个文本框
    EditText input;
    TextView show;
    // 定义界面上的两个个按钮
    Button inter;
    Button send;
    OutputStream os;
    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        input = findViewById(R.id.input);
        inter = findViewById(R.id.inter);
        send = findViewById(R.id.send);
        show = findViewById(R.id.show);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                // 如果消息来自于子线程
                if (msg.what == 0x123) {
                    // 将读取的内容追加显示在文本框中
                    show.setText(msg.getData().getString("receive_content"));
                }
            }
        };
        try {
            new Thread(new ClientThread(handler)).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 将用户在文本框内输入的内容写入网络
                Message msg = new Message();
                msg.what = 0x123;
                Bundle bundle = new Bundle();
                bundle.putString("send_content", input.getText().toString() + "\n");
                msg.setData(bundle);
                ClientThread.SendHandler.sendMessage(msg);
                // 清空input文本框
                input.setText("");
            }
        });
        inter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    new Thread(new ClientThread(handler)).start();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    static class ClientThread implements  Runnable{
        //该线程负责处理的Socket
        Socket s;
        Handler handler=null;
        public static Handler SendHandler=null;
        BufferedReader br=null;
        OutputStream os=null;
        public ClientThread(Handler handler) throws IOException {
            this.handler=handler;
        }

        @Override
        public void run() {
            try {
                s=new Socket("127.0.0.1",40000);
                br=new BufferedReader(new InputStreamReader(s.getInputStream()));
                os=s.getOutputStream();
                new Thread(){
                    @Override
                    public void run() {
                        String content=null;
                        try {
                            //不断读取Socket输入流中的内容。
                            while ((content = br.readLine()) != null) {
                                // 每当读到来自服务器的数据之后，发送消息通知程序界面显示该数据
                                Message msg = new Message();
                                msg.what = 0x123;
                                Bundle bundle = new Bundle();
                                bundle.putString("receive_content", content);
                                msg.setData(bundle);

                                handler.sendMessage(msg);

                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Looper.prepare();
            SendHandler=new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    if(msg.what==0x123){
                        try {
                            os.write(msg.getData().getString("send_content").getBytes());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            Looper.loop();

        }
    }}
